podTemplate(
        containers: [
                containerTemplate(
                        name: 'jnlp',
                        image: 'core.harbor.cz.shenlan.com/dbp-edu/jenkins-slave-jdk8:v1',
                        alwaysPullImage: true,
                        runAsUser: '0',
                        runAsGroup: '0',
                        workingDir: '/home/jenkins',
                )
        ],
        yaml: '''
spec: 
  tolerations:
  - effect: "NoSchedule"
    key: "node-role.kubernetes.io/master"
    operator: "Exists"
''',
        imagePullSecrets: ['harbor-secret'],
        label: 'jenkins-slave-jdk8',
        volumes: [
                persistentVolumeClaim(mountPath: '/home/jenkins', claimName: 'jenkins-pvc-slave-jdk8', readOnly: false),
                hostPathVolume(mountPath:'/usr/bin/docker', hostPath:'/usr/bin/docker'),
                hostPathVolume(mountPath:'/var/run/docker.sock', hostPath:'/var/run/docker.sock'),
                hostPathVolume(mountPath:'/var/lib/docker', hostPath:'/var/lib/docker'),
                hostPathVolume(mountPath:'/opt/var/lib/docker', hostPath:'/opt/var/lib/docker'),
        ]) {

    node('jenkins-slave-jdk8') {
        // 构建标签：生成的docker镜像的标签，不指定时默认用 BUILD_NUMBER 代替
        def buildTag = "${DOCKER_IMAGE_TAG == '' ? BUILD_NUMBER : DOCKER_IMAGE_TAG}"

        def gitRepo = 'ssh://gerrit.deepblueai.com:29418/HRMS'
        def gitCredentialsId = 'gerrit-credential'
        def getBranch = 'master'

        def registryHost = 'core.harbor.cz.shenlan.com'
        def registryGroup = 'dbp-edu'
        def registryCredential = 'harbor-credential'

        def serviceDirMap = [
                "user-app": "user-app",
                "hr-app": "hr-app"
        ]

        def buildTasks=[:]

        stage('git fetch') {
            checkout([
                    $class: 'GitSCM',
                    branches: [[name: getBranch]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [
                            // [$class: 'CleanCheckout'], // 会删除 .node_modules目录，导致不能使用缓存加速构建，所以去掉此选项
                            [$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true, timeout: 15]
                    ],
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: gitCredentialsId, url: gitRepo]]
            ])
            sh 'git clean  -df'
        }

        stage('maven build') {
            dir("./backend") {
                sh 'mvn clean install -Dmaven.repo.local=/home/jenkins/mvn_repo/'
            }
        }

        stage('parallel build & push images') {
            def services = params.BUILD_MODULES.split(",")
            println "services need to build:${params.BUILD_MODULES}"
            services.each{ svcName ->
                buildTasks["${svcName}"] = {
                    def svcDir = serviceDirMap.get(svcName as String)
                    def mainDir = "./backend/${svcDir}"
                    println "svcName:[${svcName}], svcDir:[${mainDir}]"
                    dir(mainDir) {
                        def serviceImage = docker.build("${registryGroup}/${svcName}:${buildTag}")
                        docker.withRegistry("https://${registryHost}", registryCredential) {
                            serviceImage.push()
                            serviceImage.push('latest')
                        }
                    }
                }
            }
            parallel buildTasks
        }
        sh "docker system prune -f"
    }
}
